package org.example.product.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.product.domain.Category;
import org.example.product.service.ICategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 产品分类Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-11
 */
@Controller
@RequestMapping("/product/productCategory")
public class CategoryController extends BaseController
{
    private String prefix = "product/productCategory";

    @Autowired
    private ICategoryService categoryService;

    @RequiresPermissions("product:productCategory:view")
    @GetMapping()
    public String productCategory()
    {
        return prefix + "/productCategory";
    }

    /**
     * 查询产品分类树列表
     */
    @RequiresPermissions("product:productCategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Category> list(Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        return list;
    }

    /**
     * 导出产品分类列表
     */
    @RequiresPermissions("product:productCategory:export")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Category category)
    {
        List<Category> list = categoryService.selectCategoryList(category);
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        return util.exportExcel(list, "产品分类数据");
    }

    /**
     * 新增产品分类
     */
    @GetMapping(value = { "/add/{categoryId}", "/add/" })
    public String add(@PathVariable(value = "categoryId", required = false) Long categoryId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(categoryId))
        {
            mmap.put("category", categoryService.selectCategoryByCategoryId(categoryId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存产品分类
     */
    @RequiresPermissions("product:productCategory:add")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Category category)
    {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改产品分类
     */
    @RequiresPermissions("product:productCategory:edit")
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        Category category = categoryService.selectCategoryByCategoryId(categoryId);
        mmap.put("category", category);
        return prefix + "/edit";
    }

}
